﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RentHouse.DTO;
using RentHouse.IService;

namespace RentHouse.Web.Controllers
{
    public class HouseController : Controller
    {
        private readonly IHouseService _houseService;
        private readonly IRegionService _regionService;
        private readonly IHouseAppointmentService _houseAppointmentService;
        public HouseController(IHouseService houseService,IRegionService regionService,IHouseAppointmentService houseAppointmentService)
        {
            this._houseService = houseService;
            this._regionService = regionService;
            this._houseAppointmentService = houseAppointmentService;
        }
        /// <summary>
        /// 房源详情页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(int id)
        {   
            //在前端显示图片的时候，由于图片是从后天上床的，所以图片保存在后台服务器中
            //我们需要通过后台的地址获取到后台服务器中的图片
            //需要1:  获取后台的地址（ip/域名）   2：保证后台项目是启动状态
            ViewBag.ServerIP = Common.CommonHelper.GetServerIP();
            return View(_houseService.GetHouseDetail(id));
        }
        
        /// <summary>
        /// 条件搜索
        /// </summary>
        /// <param name="houseSearch"></param>
        /// <returns></returns>
        public ActionResult Search(HouseSearchOptions houseSearch)
        {
            long cityId = 1;
            ViewBag.RegionList = _regionService.GetRegionList(cityId);
            houseSearch.CityId = cityId;
            //按照月租过滤
            if (!string.IsNullOrWhiteSpace(houseSearch.MonthRent))          //100-200  *-100  100-*
            {
                var strMonthRent = houseSearch.MonthRent.Split('-');   //100    200
                if (strMonthRent[0]!="*")
                {
                    houseSearch.StartMonthRent = Convert.ToInt32(strMonthRent[0]);
                }
                if (strMonthRent[1]!="*")
                {
                    houseSearch.EndMonthRent = Convert.ToInt32(strMonthRent[1]);
                }
            }
            return View(_houseService.GetListBySearch(houseSearch));
        }


        public ActionResult Add(HouseAppointmentAddDTO dto)
        {
            return Json(_houseAppointmentService.Add(dto));
        
        }


    }
}